package presstrack.targets
{
	import flash.net.Responder;	
	import mx.collections.ArrayCollection;
	import mx.utils.ObjectUtil;
	
	import org.presstrack.service.CrudService;
	
	[RemoteClass(alias="presstrack.targets.TargetClass")]
	
	[Bindable]
	public class TargetClass extends Object
	{
		// public 
		public var iTargetId	:int; 		
		public var mStartDate	:String;
		public var mStartTime	:String;
		public var iTimeLength	:int;
		public var mNote		:String;
		
		public var iTaskId		:int;
		public var mTaskLabel	:String;
		public var iTaskSection	:int;
		public var mProduct		:String;
		
		public var iOrderId		:int;
		public var iOrderLabel	:String;
		public var mCustomer	:String;	
		
		public var obStartDate	:Date;	
			
		public var end_date:Date;		
		
		public function TargetClass(ob:Object = null)
		{
			if (ob != null)
			{
				copyFrom(ob);
			}
		}
		
		public function copyFrom(ob:Object):void 
		{
			if (ob.hasOwnProperty('iTargetId'))
			{
				iTargetId = ob.iTargetId;
			}
				
			if (ob.hasOwnProperty('mStartDate'))
			{
				mStartDate = ob.mStartDate;
				setStartDate();
			}
				
			if (ob.hasOwnProperty('mStartTime'))
			{
				mStartTime = ob.mStartTime;
			}
				
			if (ob.hasOwnProperty('iTimeLength'))
			{
				iTimeLength = ob.iTimeLength;
			}
				
			if (ob.hasOwnProperty('mNote'))
			{
				mNote = ob.mNote;
			}
				
			if (ob.hasOwnProperty('iTaskId'))
			{
				iTaskId = ob.iTaskId;
			}
			
			if (ob.hasOwnProperty('mTaskLabel'))
			{
				mTaskLabel = ob.mTaskLabel;
			}
				
			if (ob.hasOwnProperty('iTaskSection'))
			{
				iTaskSection = ob.iTaskSection;
			}
				
			if (ob.hasOwnProperty('iOrderId'))
			{
				iOrderId = ob.iOrderId;
			}
				
			if (ob.hasOwnProperty('iOrderLabel'))
			{
				iOrderLabel = ob.iOrderLabel;
			}
				
			if (ob.hasOwnProperty('mCustomer'))
			{
				mCustomer = ob.mCustomer;
			}
				
			if (ob.hasOwnProperty('mProduct'))
			{
				mProduct = ob.mProduct;	
			}				
		}
		
		public function compare(a:Object):int
        {
        	if (mStartDate == "-/-" || mStartTime == "--:--")
        	{
       			return 0;
       		}
       			
        	if (a.mStartDate == "-/-" || a.mStartTime == "--:--")
        	{
       			return -1;
       		}
     
     		if (a.obStartDate.getTime() > obStartDate.getTime())
     		{
           		return -1;
       		}
           	else
           	{
     			return 1;
           	}
            return 0;
     	}
		
		public function setTarget(day		:String, 
								  time		:String, 
								  length	:int, 
								  note		:String, 
								  resFunc	:Function = null, 
								  faultFunc	:Function = null):void 
		{
			CrudService.crud.srv.call("PressTrack.updateTarget", new Responder(onResult, onFault), 
									  iTargetId, day, time, length, note);
			
			function onResult(res:String):void 
			{
				if (res == 'successful') 
				{
					mStartDate	= day;
					mStartTime	= time;
					iTimeLength = iTimeLength;
					mNote 		= mNote;
					setStartDate();
					
					if (resFunc != null)
					{
						resFunc();
					}
				}
				else	
				{
					faultFunc(res);	
				}		
			}
			
			function onFault(e:Object):void 
			{
				faultFunc(ObjectUtil.toString(e));
			}
		}
		
		// private 
		
		protected function setStartDate():Boolean
		{
			if (mStartDate != "-/-" && mStartTime != "--:--")
			{
				obStartDate = new Date(2011, 
									   mStartDate.split("/")[1], 
									   mStartDate.split("/")[0], 
									   mStartTime.split(":")[0], 
									   mStartTime.split(":")[1]);
				return true;
			}				
			return false;
		}
	}
}